const validate = (schema) => {
  return async (ctx, next) => {
    try {
      const { error } = schema.validate(ctx.request.body);
      if (error) {
        ctx.status = 400;
        ctx.body = {
          code: 400,
          message: error.details[0].message
        };
        return;
      }
      await next();
    } catch (error) {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        message: '服务器错误'
      };
    }
  };
};

module.exports = validate; 